package com.chijun.mform.process.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.chijun.mform.FormUtils;
import com.chijun.mform.R;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.create.mode.FormFieldOption;
import com.chijun.mform.field.BaseField;
import com.chijun.mform.field.FieldFullViewUtil;
import com.chijun.mform.process.IHandlerLoadFormFull;
import com.chijun.mform.process.ModeServerFullData;
import com.chijun.mform.process.OptionChangeListener;
import com.example.ccj.b.a.LocationMapActivity;
import com.example.ccj.b.c.dialog.ErrorDialog;
import com.example.ccj.b.c.dialog.LoadingDialog;
import com.example.ccj.u.FileUtils;
import com.example.ccj.u.message.UiMessageUtils;
import com.example.ccj.u.server.MyObserver;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 加载填写表单的字段
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/31 14:52
 */
public class ViewLoadFullFieldData extends LinearLayout implements OptionChangeListener {

    ModeServerFullData serverFullData;
    IHandlerLoadFormFull iHandlerLoadFormFull;

    public ViewLoadFullFieldData(Context context, ModeServerFullData serverFullData, IHandlerLoadFormFull iHandlerLoadFormFull) {
        super(context);
        this.serverFullData = serverFullData;
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;
        init();
    }

    public ViewLoadFullFieldData(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void setData(ModeServerFullData serverFullData,IHandlerLoadFormFull iHandlerLoadFormFull){
        this.serverFullData = serverFullData;
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;
        init();
    }

    Map<Long,View> allField; //字段相关viewMap,用于选项改变时隐藏显示关联字段

    private void init(){
        allField = new HashMap<>();
        inflate(getContext(), R.layout.view_load_full_data,this);
        LinearLayout layout = findViewById(R.id.view_load_full_data_field_data);

        //子线程加载字段数据
        Observable.create(new ObservableOnSubscribe<View>() {
                    @Override
                    public void subscribe(ObservableEmitter<View> e) throws Exception {
                        //字段数据
                        for (FormField field : serverFullData.formFieldList) {
                            BaseField baseField = FieldFullViewUtil.getFieldModelByType(field.getType());
                            View fieldFullView = baseField.getShowFullView(getContext(),field,serverFullData,ViewLoadFullFieldData.this,iHandlerLoadFormFull);
                            allField.put(field.getId(),fieldFullView);
                            e.onNext(fieldFullView);
                        }
                        e.onComplete();
                    }
                }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<View>() {
                    @Override
                    public void onNext(View fieldView) {
                        if (fieldView != null) layout.addView(fieldView);
                    }
                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ErrorDialog errorDialog = new ErrorDialog(getContext(),e.getMessage());
                        errorDialog.show();
                    }
                });
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //销毁时取消消息监听
        UiMessageUtils.getInstance().removeListeners(FileUtils.PICK_FILE_RESULT);
        UiMessageUtils.getInstance().removeListeners(LocationMapActivity.RESULT_LOCATION_CODE);
    }

    //选项发生改变时
    @Override
    public void onChanged(long fieldId, FormFieldOption... formFieldOptionList) {
        List<Long> allOptionId = new ArrayList<>();

        for (List<FormFieldOption> value : serverFullData.formFieldOptionMap.values()) {
            for (FormFieldOption formFieldOption : value) {
                allOptionId.add(formFieldOption.id);
            }
        }
        Map<Long,Long[]> formOptionAllReFieldMap = new HashMap<>();
        for (Long l : serverFullData.formOptionReFieldMap.keySet()) {
            List<Long> longs = serverFullData.formOptionReFieldMap.get(l);
            if (longs != null) formOptionAllReFieldMap.put(l, longs.toArray(new Long[0]));
        }
        Long[] selectOptionIdArray = new Long[formFieldOptionList.length];
        for (int i = 0; i < formFieldOptionList.length; i++) {
            selectOptionIdArray[i] = formFieldOptionList[i].id;
        }
        FormUtils.FieldOptionChanged(allField,allOptionId,formOptionAllReFieldMap,selectOptionIdArray);
    }
}
